﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
namespace DMOZ
{
   public class Client
    {
      private   bool _successful = false;
      private string _url;
      private string _strRegex;
      public   bool Success
      {
          get { return _successful; }
      }
      public Client(string url, string strRegex)
       {
           _url = url;
           _strRegex = strRegex;
       }
       
      public   List<Page> Request()
       {
           List<Page> result = new List<Page>(); ;
           try
           {
               HtmlWeb webClient = new HtmlWeb();
               HtmlAgilityPack.HtmlDocument doc = webClient.Load(_url);

               doc.LoadHtml(doc.DocumentNode.SelectSingleNode(_strRegex).InnerHtml);
               HtmlNodeCollection nodeHeaders = doc.DocumentNode.SelectNodes("./li");

               Page page = null;
               foreach (HtmlNode node in nodeHeaders)
               {
                   page = new Page();
                   string[] nodeText = node.InnerText.Trim().Split('-');
                   page.title = nodeText[0];
                   page.desc = nodeText[1];
                   page.url = node.SelectSingleNode("./a").Attributes["href"].Value;
                   result.Add(page);
               }

               doc = null;
               nodeHeaders = null;
               webClient = null;
               _successful = true;
             
           }
           catch
           {
               throw;
               _successful = false;
           }
           return result;
      }

     
   
    }
}
